Skip to main content

Spring Boot application.yml Configuration Guide

Overviewโ€‹

The application.yml file is the primary configuration file in Spring Boot applications. It uses YAML syntax to define application properties, making it more readable and hierarchical compared to the traditional application.properties file.

Server Configurationโ€‹

Control embedded server settings like port, context path, and SSL.

server:
port: 8080
servlet:
context-path: /api
session:
timeout: 30m
compression:
enabled: true
mime-types: text/html,text/xml,text/plain,application/json
ssl:
enabled: false
key-store: classpath:keystore.p12
key-store-password: changeit
key-store-type: PKCS12
error:
include-message: always
include-binding-errors: always

Database Configurationโ€‹

DataSource Propertiesโ€‹

Configure database connections for various databases.

spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: dbuser
password: dbpass
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 10
minimum-idle: 5
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000

JPA/Hibernate Configurationโ€‹

spring:
jpa:
hibernate:
ddl-auto: update # Options: none, validate, update, create, create-drop
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
use_sql_comments: true
open-in-view: false

Logging Configurationโ€‹

Control logging levels and output formats.

logging:
level:
root: INFO
com.mycompany: DEBUG
org.springframework.web: DEBUG
org.hibernate.SQL: DEBUG
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
file:
name: logs/application.log
max-size: 10MB
max-history: 30

Application Propertiesโ€‹

Define custom application-level configurations.

spring:
application:
name: my-spring-app
profiles:
active: dev

Security Configurationโ€‹

Basic security and authentication settings.

spring:
security:
user:
name: admin
password: admin123
roles: ADMIN,USER
oauth2:
client:
registration:
google:
client-id: your-client-id
client-secret: your-client-secret
scope: openid,profile,email

Custom Variablesโ€‹

Define your own application-specific variables.

app:
name: My Application
version: 1.0.0
api:
base-url: https://api.example.com
timeout: 5000
features:
email-enabled: true
max-upload-size: 10485760 # 10MB in bytes
admin:
email: admin@example.com

Access these in your code using @Value or @ConfigurationProperties:

@Value("${app.api.base-url}")
private String apiBaseUrl;

Cache Configurationโ€‹

Configure caching mechanisms.

spring:
cache:
type: redis
redis:
time-to-live: 600000
cache-names: users,products,orders
redis:
host: localhost
port: 6379
password: redis-password
timeout: 2000ms

Mail Configurationโ€‹

SMTP server settings for sending emails.

spring:
mail:
host: smtp.gmail.com
port: 587
username: your-email@gmail.com
password: your-app-password
properties:
mail:
smtp:
auth: true
starttls:
enable: true

Message Queue Configurationโ€‹

RabbitMQโ€‹

spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /

Kafkaโ€‹

spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: my-consumer-group
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer

File Upload Configurationโ€‹

spring:
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 10MB
file-size-threshold: 2KB

Actuator Configurationโ€‹

Monitor and manage your application.

management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
base-path: /actuator
endpoint:
health:
show-details: always
metrics:
export:
prometheus:
enabled: true

Profile-Specific Configurationโ€‹

Create separate configurations for different environments.

application-dev.ymlโ€‹

spring:
datasource:
url: jdbc:postgresql://localhost:5432/devdb
jpa:
show-sql: true
logging:
level:
root: DEBUG

application-prod.ymlโ€‹

spring:
datasource:
url: jdbc:postgresql://prod-server:5432/proddb
jpa:
show-sql: false
logging:
level:
root: WARN

Activate profiles using:

spring:
profiles:
active: prod

Or via command line: java -jar app.jar --spring.profiles.active=prod

Jackson Configurationโ€‹

Configure JSON serialization/deserialization.

spring:
jackson:
serialization:
indent-output: true
write-dates-as-timestamps: false
deserialization:
fail-on-unknown-properties: false
default-property-inclusion: non_null
time-zone: UTC

Internationalizationโ€‹

spring:
messages:
basename: messages
encoding: UTF-8
cache-duration: 3600

Complete Exampleโ€‹

Here's a comprehensive example combining commonly used configurations:

# Application Configuration
spring:
application:
name: my-spring-boot-app
profiles:
active: dev

# Server Configuration
server:
port: 8080
servlet:
context-path: /api
compression:
enabled: true

# Database Configuration
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: ${DB_USERNAME:postgres}
password: ${DB_PASSWORD:postgres}
hikari:
maximum-pool-size: 10
jpa:
hibernate:
ddl-auto: validate
show-sql: true

# Logging
logging:
level:
root: INFO
com.mycompany: DEBUG
file:
name: logs/app.log

# Custom Properties
app:
name: My Application
version: 1.0.0
api:
key: ${API_KEY:default-key}
timeout: 5000

# Actuator
management:
endpoints:
web:
exposure:
include: health,info,metrics

Best Practicesโ€‹

  1. Use Environment Variables: Reference sensitive data using ${ENV_VAR:default-value} syntax
  2. Profile Separation: Keep environment-specific configs in separate profile files
  3. Documentation: Add comments to explain non-obvious configuration choices
  4. Security: Never commit passwords or API keys; use environment variables or secret managers
  5. Validation: Use @Validated and @ConfigurationProperties for type-safe configuration
  6. Defaults: Always provide sensible default values for optional properties

Common Configuration Properties Referenceโ€‹

  • spring.datasource.*: Database connection settings
  • spring.jpa.*: JPA and Hibernate settings
  • server.*: Embedded server configuration
  • logging.*: Logging configuration
  • management.*: Actuator and monitoring
  • spring.security.*: Security settings
  • spring.cache.*: Caching configuration
  • spring.kafka.*: Kafka messaging
  • spring.mail.*: Email configuration